$(function () {

    $.ajax({
        type: 'get',
        url: 'http://localhost:8080/api/v1/admin/category/list',
        success: function (backData) {
            // console.log(backData);
            if (backData.code == 200) {
                let data = backData.data;
                // 2. 结合模板引擎,生成页面结构
                let htmlStr = template('tpl-category', data);
                $('#selCategory').html(htmlStr);
            }
        }
    })



    let selpage = 1;

    function loadData(page) {
        $.ajax({
            type: 'get',
            url: 'http://localhost:8080/api/v1/admin/article/query',
            data: {
                key: '',
                type: $('#selCategory').val(),
                state: $('#selStatus').val(),
                page: page,
                perpage: 10,
            },
            success: function (backData) {
                if (backData.code == 200) {
                    // console.log(backData);
                    if (backData.data.totalCount > 0) {
                        let dataArr = backData.data.data;
                        let htmlStr = template('tpl-list', dataArr);
                        $('tbody').html(htmlStr);


                        $('#pagination').twbsPagination('destroy');


                        $('#pagination').twbsPagination({
                            totalPages: backData.data.totalPage,
                            visiblePages: 5,
                            startPage: page,
                            initiateStartPageClick: false,
                            first: '首页',
                            last: '尾页',
                            prev: '上一页',
                            next: '下一页',
                            onPageClick: function (event, page) {
                                selpage = page;
                                loadData(page);
                            }
                        });
                    } else {
                        $('tbody').empty();
                        $('#pagination').empty();
                        $('#pagination').html('现今还未有数据，不好意思，请等待更新！！！！')
                    }
                }
            }
        })
    }
    loadData(1);


    $('#btnSearch').on('click', function (event) {
        event.preventDefault();
        loadData(1);
    });




    $('tbody').on('click', '.delete', function () {
        $.ajax({
            type: 'post',
            url: 'http://localhost:8080/api/v1/admin/article/delete',
            data: {
                id: $(this).attr('data-id'),
            },
            success: function (backData) {
                console.log(backData);
                if (backData.code == 204) {
                    if ($('tbody tr').length == 1 && selpage != 1) {
                        loadData(--selpage);
                    } else {
                        loadData(selpage);
                    }
                }
            }
        })
    });




    $('tbody').on('click', '.btn-editer', function () {
        window.localStorage.setItem('article-id', $(this).attr('data-id'));
    });
})